using Microsoft.AspNetCore.Mvc;
using MediPlatform.Read.Api.Models;
using MediPlatform.Read.Api.Services;
using MediPlatform.ErrorCode;

namespace MediPlatform.Read.Api.Controllers
{
    /// <summary>
    /// 问诊单管理控制器
    /// </summary>
    [ApiController]
    [Route("api/[controller]")]
    public class ConsultationOrderController : ControllerBase
    {
        private readonly ConsultationOrderService _consultationOrderService;
        private readonly ILogger<ConsultationOrderController> _logger;

        public ConsultationOrderController(ConsultationOrderService consultationOrderService, ILogger<ConsultationOrderController> logger)
        {
            _consultationOrderService = consultationOrderService;
            _logger = logger;
        }

        /// <summary>
        /// 获取问诊单分页列表
        /// </summary>
        /// <param name="request">查询请求</param>
        /// <returns>分页结果</returns>
        [HttpGet("list")]
        public async Task<ApiResult<ConsultationOrderPagedResponse>> GetPagedList([FromQuery] ConsultationOrderQueryRequest request)
        {
            try
            {
                _logger.LogInformation("收到问诊单分页查询请求，页码：{PageIndex}，页大小：{PageSize}", request.PageIndex, request.PageSize);

                var result = await _consultationOrderService.GetPagedListAsync(request);

                return ApiResult<ConsultationOrderPagedResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取问诊单分页列表失败");
                return ApiResult<ConsultationOrderPagedResponse>.Error("获取问诊单列表失败");
            }
        }

        /// <summary>
        /// 获取问诊单详情
        /// </summary>
        /// <param name="id">问诊单ID</param>
        /// <returns>问诊单详情</returns>
        [HttpGet("detail/{id}")]
        public async Task<ApiResult<ConsultationOrderDetailResponse>> GetDetail(long id)
        {
            try
            {
                _logger.LogInformation("收到问诊单详情查询请求，问诊单ID：{OrderId}", id);

                if (id <= 0)
                {
                    return ApiResult<ConsultationOrderDetailResponse>.Error("问诊单ID无效");
                }

                var result = await _consultationOrderService.GetDetailAsync(id);

                if (result == null)
                {
                    return ApiResult<ConsultationOrderDetailResponse>.Error("问诊单不存在");
                }

                return ApiResult<ConsultationOrderDetailResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取问诊单详情失败，问诊单ID：{OrderId}", id);
                return ApiResult<ConsultationOrderDetailResponse>.Error("获取问诊单详情失败");
            }
        }

        /// <summary>
        /// 获取问诊单统计数据
        /// </summary>
        /// <returns>统计数据</returns>
        [HttpGet("statistics")]
        public async Task<ApiResult<ConsultationOrderStatisticsResponse>> GetStatistics()
        {
            try
            {
                _logger.LogInformation("收到问诊单统计查询请求");

                var result = await _consultationOrderService.GetStatisticsAsync();

                return ApiResult<ConsultationOrderStatisticsResponse>.Success(result);
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "获取问诊单统计数据失败");
                return ApiResult<ConsultationOrderStatisticsResponse>.Error("获取统计数据失败");
            }
        }

        /// <summary>
        /// 批量删除问诊单
        /// </summary>
        /// <param name="ids">问诊单ID列表</param>
        /// <returns>操作结果</returns>
        [HttpDelete("batch")]
        public async Task<ApiResult<object>> BatchDelete([FromBody] List<long> ids)
        {
            try
            {
                _logger.LogInformation("收到批量删除问诊单请求，数量：{Count}", ids.Count);

                if (ids == null || !ids.Any())
                {
                    return ApiResult<object>.Error("请选择要删除的问诊单");
                }

                // 这里可以添加批量删除的逻辑
                // 暂时返回成功，实际项目中需要实现具体的删除逻辑
                _logger.LogInformation("批量删除问诊单成功，数量：{Count}", ids.Count);

                return ApiResult<object>.Success("批量删除成功");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "批量删除问诊单失败");
                return ApiResult<object>.Error("批量删除失败");
            }
        }

        /// <summary>
        /// 删除单个问诊单
        /// </summary>
        /// <param name="id">问诊单ID</param>
        /// <returns>操作结果</returns>
        [HttpDelete("{id}")]
        public async Task<ApiResult<object>> Delete(long id)
        {
            try
            {
                _logger.LogInformation("收到删除问诊单请求，问诊单ID：{OrderId}", id);

                if (id <= 0)
                {
                    return ApiResult<object>.Error("问诊单ID无效");
                }

                // 这里可以添加删除的逻辑
                // 暂时返回成功，实际项目中需要实现具体的删除逻辑
                _logger.LogInformation("删除问诊单成功，问诊单ID：{OrderId}", id);

                return ApiResult<object>.Success("删除成功");
            }
            catch (Exception ex)
            {
                _logger.LogError(ex, "删除问诊单失败，问诊单ID：{OrderId}", id);
                return ApiResult<object>.Error("删除失败");
            }
        }
    }
} 